<template>
	<view>
		<!-- <hx-navbar title="收货地址" :back="true" :fixed="true" height="88rpx"></hx-navbar> -->
		<view class="tp-w690 tp-fyc noaddress" v-if="!haveAddress">
			<view class="noaddressimg column_align">
				<image class="tp-mb20" src="static/no-address.png" mode=""></image>
				暂无地址，请先添加地址
			</view>
			<view class="btn_location">
				<button class="tp-btn-h88 submitbtn" @click="goAddressEdit">添加地址</button>
			</view>
		</view>
		<view class="haveaddrss tp-w690" v-else>
			<view class="list">
				<view class="item tp-mt20 tp-bgf tp-br15" v-for="(item, index) in addressList" :key="index">
					<view class="content tp-bdbm" @click="navigateToUrl(item)">
						<view class="nameandtel tp-fxc">
							<text class="ellispsis1 name">收货人：{{item.name}}</text>
							<text class="tel">{{item.phone}}</text>
						</view>
						<view class="address-detail tp-mt20">
							{{item.province}}{{item.city}}{{item.area}}{{item.address}}
						</view>
					</view>
					<view class="btm-wrap tp-fxc">
						<view class="defalutaddress tp-fxc" @click="setDefaultAddress(item)">
							<view class="icon tp-mr20">
								<image v-if="item.status === '1'" src="static/choose0-icon.png" mode=""></image>
								<image v-else src="static/choose1-icon.png" mode=""></image>
							</view>
							<text :style="{color:item.status === '1'?'':'#FE4543'}">默认地址</text>
						</view>
						<view class="editanddel-wrap tp-fxc">
							<view class="edit tp-fxc" @click="goEditAddress(item)">
								<view class="icon tp-mr20">
									<image src="../static/imgs/icons/edit-icon.png" mode=""></image>
								</view>
								<text>编辑</text>
							</view>
							<view class="del tp-fxc" @click="deleteAddress(item.id)">
								<view class="icon tp-mr20">
									<image src="../static/imgs/icons/edit-icon.png" mode=""></image>
								</view>
								<text>删除</text>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="tp-h98"> </view>
		<view class="tp-h56">
			
		</view>
		<!-- 新增地址按钮S -->
		<view v-if="haveAddress" class="confirm-btn" @click="goAddressEdit">新增地址</view>
		<!-- 新增地址按钮E -->
	</view>
</template>

<script>
	export default {
		components: {
		},
		data() {
			return {
				addressList: [], // 地址列表数据
				haveAddress: false, // 默认有地址
				backUrl: false, // 是否需要返回上一步
			};
		},

		onLoad(opt) {
			if (opt.src) {
				this.backUrl = opt.src
			}
		},

		onShow() {
			this.getAddressList()
		},

		methods: {
			// 获取全部收货地址
			getAddressList() {
				this.$http.post(this.$api.user_address_1).then(result => {
					if(result.code === 200) {
						this.addressList = result.data;
						this.haveAddress = true
					} else {
						this.addressList = []
						this.haveAddress = false
					}
				})
			},
			navigateToUrl(item) { // 点击选择地址后,  返回上一步
				uni.setStorage({
				    key: 'temporary_adderss',
				    data: item,
				    success: function () {
				        console.log('success');
				    }
				});
				// 商城订单选择地址
				const lastpath = this.getLastPagePath()
				console.log('上一页',lastpath);
				if(
					lastpath === 'pagesShop/order/submit' 
					|| lastpath === 'pagesShop/order/ms_submit' 
					|| lastpath === 'pagesShop/order/tg_submit' 
					|| lastpath === 'pagesShop/order/jifenSubmit'
					|| lastpath === 'pagesShop/details/kj_details'
				) {
					uni.$emit('select-address',item)
				}
				uni.navigateBack({
					delta: 1
				});
			},

			goAddressEdit() { // 去添加地址
				uni.navigateTo({
					url: './addressEdit'
				});
			},
			goEditAddress(obj) { // 去编辑地址
				uni.navigateTo({
					url: './addressEdit?obj=' + JSON.stringify(obj)
				});
			},
			// 设置默认地址
			setDefaultAddress(item) { 
				this.$http.post(this.$api.user_address_3, {}, {
					params: {
						id: item.id,
					}
				}).then(result => {
					uni.showToast({
						title: result.msg,
						icon:'none'
					});
					if(result.code === 200) {
						this.getAddressList()
					}
				})
			},
			// 删除地址
			deleteAddress(ids) {
				this.$http.post(this.$api.user_address_2, {}, {
					params: {
						id: ids
					}
				}).then(result => {
					uni.showToast({
						title: result.msg,
						icon:'none'
					});
					if(result.code === 200) {
						this.getAddressList()
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.noaddress {
		.noaddressimg {
			height: 318rpx;
			width: 358rpx;
			margin: 296rpx auto;
		}

		.btn_location {
			position: absolute;
			bottom: 120rpx;
			left: 50%;
			transform: translateX(-50%);
			width: 80%;
		}

		.submitbtn {
			background-color: $tc;
			border-radius: 44rpx;
		}
	}

	.haveaddrss {
		.list {
			.item {
				.content {
					padding: 30rpx 20rpx;

					.nameandtel {
						width: 100%;
						font-size: 28rpx;
						justify-content: space-between;

						.name {
							flex: 1;
						}

						.tel {
							width: 200rpx;
							text-align: right;
						}
					}

					.address-detail {
						font-size: 26rpx;
						color: #999;
						line-height: 40rpx;
					}
				}

				.btm-wrap {
					padding: 30rpx;
					justify-content: space-between;

					.defalutaddress {
						&.active {
							color: $tc;
						}

						.icon {
							height: 28rpx;
							width: 28rpx;
						}
					}

					.editanddel-wrap {
						.icon {
							height: 28rpx;
							width: 28rpx;
						}

						.edit {
							margin-right: 40rpx;
						}
					}
				}
			}
		}
	}

	.confirm-btn {
		width: 100%;
		background-color: $tc;
		color: #fff;
		text-align: center;
		height: 100rpx;
		line-height: 100rpx;
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		font-size: 32rpx;
	}
</style>
